using System;
using System.Configuration;
using System.Diagnostics;
using BIUtilities.Publish;
using BIUtilities.Publish.CustomConfiguration;

namespace BIUtilities.ReportPublisher
{
    /// <summary>
    /// Entrypoint classs for tool which will read the config settings and do necessary deployments
    /// </summary>
    public class ReportPublisher
    {
        static void Main(string[] args)
        {
            DeploymentSettingsElement deploymentSettings =
                (DeploymentSettingsElement)ConfigurationManager.GetSection("deploymentSettings");

            CommandLineParams paramData = ParseCommandLine(args, deploymentSettings);

            // Reads in the only command line param, which config to use
            string deploymentType = paramData.DeploymentType.ToUpper();

            // Matches the key entered with the deployments which are in the config file
            foreach (DeploymentElement deployConfig in deploymentSettings.Deployments)
            {
                if (deployConfig.Type.ToUpper() == deploymentType)
                {
                    // Deploy everything
                    ParameterReplacement.UpdateConfigWithReplacements(deployConfig, paramData);
                    DeploymentManager mgr = new DeploymentManager();
                    mgr.DeployConfiguration(deployConfig);
                    break;
                }
            }
        }



        /// <summary>
        /// Parse the command line entered for the app and find the deployment type and replacements
        /// </summary>
        /// <param name="args"></param>
        /// <param name="deploymentSettings"></param>
        /// <returns></returns>
        private static CommandLineParams ParseCommandLine(string[] args, DeploymentSettingsElement deploymentSettings)
        {
            CommandLineParams paramData = new CommandLineParams();
            paramData.Initialize();

            if (args.Length == 1)
            {
                // If not using params, then just take the string as the deploy type
                string singleParam = args[0];
                if (singleParam.ToUpper().StartsWith("/D:"))
                    paramData.DeploymentType = singleParam.Substring(3);
                else
                    paramData.DeploymentType = singleParam;
            }
            else if (args.Length > 1)
            {
                // Go through the params and pull out what they all are.
                foreach (string arg in args)
                {
                    if ((arg.Length > 3) && (arg.StartsWith("/")) && (arg[2] == ':'))
                    {
                        switch (arg[1].ToString().ToUpper())
                        {
                            case "D":
                                paramData.DeploymentType = arg.Substring(3);
                                break;
                            case "P":
                                string paramValue = arg.Substring(3);
                                int firstEqualSign = paramValue.IndexOf("=");
                                if (firstEqualSign > 0)
                                {
                                    string paramName = paramValue.Substring(0, firstEqualSign);
                                    string paramNameReplace = paramValue.Substring(firstEqualSign+1);
                                    paramData.ReplacementList.Add(paramName, paramNameReplace);
                                    paramData.ReplacementCount.Add(paramName, 0);
                                }
                                break;
                        }
                    }
                }
            }

            if (paramData.DeploymentType.Length == 0)
            {
                Console.WriteLine("You must specify a configuration deployment type. Acceptable commandlines are as follows:\n");

                // Matches the key entered with the deployments which are in the config file
                foreach (DeploymentElement deployConfig in deploymentSettings.Deployments)
                {
                    Console.WriteLine("\t" + Environment.CommandLine + " /d:" + deployConfig.Type.ToString());
                }
                Environment.Exit(1);
            }

            return paramData;
        }

    }
}
